/**
 * 
 */
package com.abel.web.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.abel.web.view.OptionalInfoViewBean;

/**
 * @author admin
 *
 */
public class OptionalInfoViewValidator implements Validator {

	/* (non-Javadoc)
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public boolean supports(Class clazz) {
		return OptionalInfoViewBean.class.equals(clazz);
	}

	/* (non-Javadoc)
	 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
	 */
	@Override
	public void validate(Object obj, Errors errors) {
		
		
		OptionalInfoViewBean viewBean = (OptionalInfoViewBean)obj;
		
		if (viewBean.getHeightInFeet() != null 
				&& !viewBean.getHeightInFeet().equals("")) {
			try {
				Integer.valueOf(viewBean.getHeightInFeet());
			}
			catch (Exception e) {
				errors.rejectValue("heightInFeet", "error.null",null,"Height(Feet) has to be a valid numeric value");
			}
		}
		
		if (viewBean.getHeightInInches() != null
				&& !viewBean.getHeightInInches().equals("")) {
			try {
				Integer.valueOf(viewBean.getHeightInInches());
			}
			catch (Exception e) {
				errors.rejectValue("heightInInches", "error.null",null,"Height(Inches) has to be a valid numeric value");
			}
		}

		if (viewBean.getWeight()  != null
				&& !viewBean.getWeight().equals("")) {
			try {
				Integer.valueOf(viewBean.getWeight());
			}
			catch (Exception e) {
				errors.rejectValue("weight", "error.null",null,"Weight has to be a valid numeric value");
			}
		}

		if (viewBean.getChildrensAges()  != null
				&& !viewBean.getWeight().equals("")) {
			if (!viewBean.getChildrensAges().matches("[1-9|,]*")) {
				errors.rejectValue("weight", "error.null",null,"Children's ages have to be valid numeric values separated by commas");
			}
		}
	}
}
